#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<Windows.h>

#define high 15
#define width 25

int canvas[high][width]={0};
int ball_x,ball_y;
int ball_vx,ball_vy;
int position_x,position_y;
int radius;
int left,right;
int score;
int oldball_x,oldball_y;
int t=0;

void gotoxy(int x,int y)
{
    HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos;
    pos.X=x;
    pos.Y=y;
    SetConsoleCursorPosition(handle,pos);
}

void HideCursor()//ÓÃÓÚÒþ²Ø¹â±ê
{
    CONSOLE_CURSOR_INFO cursor_info={1,0};
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
}
void startup()
{
   int i,j;
   position_x=high-2;
   position_y=width/2;
   ball_vx=1;
   ball_vy=1;
   for(i=0;i<high;i++)
       canvas[i][width-1]=-1;
   for(j=0;j<width;j++)
       canvas[high-1][j]=-2;
   for(i=0;i<4;i++)
   {
       for(j=0;j<width-1;j++)
       {
           canvas[i][j]=1;
       }
   }
   ball_x=5;
   ball_y=1;
   canvas[ball_x][ball_y]=2;
   radius=5;
   left=position_y-radius;
   right=position_y+radius;
   for(i=left;i<=right;i++)
       canvas[position_x][i]=3;


   HideCursor();
}

void show()
{
   gotoxy(0,0);
   int i,j;
   for(i=0;i<high;i++)
   {
       for(j=0;j<width;j++)
	   {
		  if(canvas[i][j]==2)
            printf("o");
          else if(canvas[i][j]==-1)
            printf("|");
          else if(canvas[i][j]==-2)
            printf("_");
          else if(canvas[i][j]==1)
            printf("#");
          else if(canvas[i][j]==3)
            printf("*");
          else if(canvas[i][j]==0)
            printf(" ");
	   }
	   printf("\n");
   }
}
void updateWithoutInput()
{
   oldball_x=ball_x;
   oldball_y=ball_y;
   ball_x=ball_x+ball_vx;
   ball_y=ball_y+ball_vy;
   canvas[oldball_x][oldball_y]=t;
   t=canvas[ball_x][ball_y];
   if(t==1)
   {
       t=0;
       ball_vx=-ball_vx;
       ball_vy=-ball_vy;
   }
   canvas[ball_x][ball_y]=2;
   if(ball_x==position_x&&(left<=ball_y&&ball_y<=right))
    ball_vx=-ball_vx;
   else if(ball_y==width-1)
    ball_vy=-ball_vy;
   else if(ball_y==0)
    ball_vy=-ball_vy;
   else if(ball_x<1)
    ball_vx=-ball_vx;
   else if(ball_x==position_x&&(ball_y<left||ball_y>right))
   {
       printf("Game Over");
       exit(0);
   }




   Sleep(50);
}
void updateWithInput()
{
   int i,j;
   char input;
   if(kbhit())
   {
	   input=getch();
	   if(input=='a'&&left!=0)
        {
            canvas[position_x][right]=0;
            position_y--;
            left=position_y-radius;
            right=position_y+radius;
            canvas[position_x][left]=3;
        }
       if(input=='d'&&right!=width)
        {
            canvas[position_x][left]=0;
            position_y++;
            left=position_y-radius;
            right=position_y+radius;
            canvas[position_x][right]=3;
        }
   }

}
int main()
{
	startup();
	while(1)
	{
		show();
		updateWithInput();
		updateWithoutInput();
	}
	return 0;
}

